/*
    FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.

    ***************************************************************************
    *                                                                         *
    * If you are:                                                             *
    *                                                                         *
    *    + New to FreeRTOS,                                                   *
    *    + Wanting to learn FreeRTOS or multitasking in general quickly       *
    *    + Looking for basic training,                                        *
    *    + Wanting to improve your FreeRTOS skills and productivity           *
    *                                                                         *
    * then take a look at the FreeRTOS books - available as PDF or paperback  *
    *                                                                         *
    *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *
    *                  http://www.FreeRTOS.org/Documentation                  *
    *                                                                         *
    * A pdf reference manual is also available.  Both are usually delivered   *
    * to your inbox within 20 minutes to two hours when purchased between 8am *
    * and 8pm GMT (although please allow up to 24 hours in case of            *
    * exceptional circumstances).  Thank you for your support!                *
    *                                                                         *
    ***************************************************************************

    This file is part of the FreeRTOS distribution.

    FreeRTOS is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License (version 2) as published by the
    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes FreeRTOS without being obliged to provide the
    source code for proprietary components outside of the FreeRTOS kernel.
    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
    more details. You should have received a copy of the GNU General Public 
    License and the FreeRTOS license exception along with FreeRTOS; if not it 
    can be viewed here: http://www.freertos.org/a00114.html and also obtained 
    by writing to Richard Barry, contact details for whom are available on the
    FreeRTOS WEB site.

    1 tab == 4 spaces!

    http://www.FreeRTOS.org - Documentation, latest information, license and
    contact details.

    http://www.SafeRTOS.com - A version that is certified for use in safety
    critical systems.

    http://www.OpenRTOS.com - Commercial support, development, porting,
    licensing and training services.
*/

/* Standard includes. */
#include <stdio.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

/* Project includes. */

#include "uart.h"

/*-----------------------------------------------------------*/

/* The time between cycles of the 'check' functionality (defined within the
tick hook. */
#define mainCHECK_DELAY         ( ( portTickType ) 5000 / portTICK_RATE_MS )

/* Bit definitions. */
#define PCONP_PCGPIO    0x00008000
#define PLLFEED_FEED1   0x000000AA
#define PLLFEED_FEED2   0x00000055
/*-----------------------------------------------------------*/
#define mainPASS_STATUS_MESSAGE         "All tasks are executing without error."

/* The WEB server has a larger stack as it utilises stack hungry string
handling library calls. */
#define mainBASIC_WEB_STACK_SIZE        ( configMINIMAL_STACK_SIZE * 4 )

/* Task priorities */
#define mainUIP_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )

/*
 * Configure the hardware for the demo.
 */
static void prvSetupHardware( void );
char *pcGetTaskStatusMessage( void );
static char *pcStatusMessage = mainPASS_STATUS_MESSAGE;

extern void vUARTInit( eBaud eWantedBaud, eParity eWantedParity, eDataBits eWantedDataBits, eStopBits eWantedStopBits, unsigned portBASE_TYPE uxBufferLength );
extern void vuIP_Task( void *pvParameters );

int main( void )
{
    /* Configure the hardware for use by this demo. */
	prvSetupHardware();

	vUARTInit(uart9600, uartNO_PARITY, uartBITS_8, uartSTOP_2, 64);
	xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );

    /* Start the scheduler. */
	vTaskStartScheduler();

    /* Will only get here if there was insufficient memory to create the idle
    task.  The idle task is created within vTaskStartScheduler(). */
	for( ;; );

	return 0;
}
/*-----------------------------------------------------------*/
char *pcGetTaskStatusMessage( void )
{
        /* Not bothered about a critical section here. */
        return pcStatusMessage;
}
/*-----------------------------------------------------------*/
void prvSetupHardware( void )
{
	/*  Setup the peripheral bus to be CCLK/4 (25 MHz). */
	LPC_SC->PCLKSEL0 = 0x00000000;

	/* Disable peripherals power. */
	LPC_SC->PCONP = 0;

	/* Enable GPIO power. */
	LPC_SC->PCONP = PCONP_PCGPIO;

	/* Disable TPIU. */
	LPC_PINCON->PINSEL10 = 0;

	if ( LPC_SC->PLL0STAT & ( 1 << 25 ) )
	{
		/* Enable PLL, disconnected. */
		LPC_SC->PLL0CON = 1;
		LPC_SC->PLL0FEED = PLLFEED_FEED1;
		LPC_SC->PLL0FEED = PLLFEED_FEED2;
	}
	
	/* Disable PLL, disconnected. */
	LPC_SC->PLL0CON = 0;
	LPC_SC->PLL0FEED = PLLFEED_FEED1;
	LPC_SC->PLL0FEED = PLLFEED_FEED2;
	    
	/* Enable main OSC. */
	LPC_SC->SCS |= 0x20;
	while( !( LPC_SC->SCS & 0x40 ) );
	
	/* select main OSC, 10MHz, as the PLL clock source. */
	LPC_SC->CLKSRCSEL = 0x01;
	
	LPC_SC->PLL0CFG = 0x10028;
	LPC_SC->PLL0FEED = PLLFEED_FEED1;
	LPC_SC->PLL0FEED = PLLFEED_FEED2;
	      
	/* Enable PLL, disconnected. */
	LPC_SC->PLL0CON = 1;
	LPC_SC->PLL0FEED = PLLFEED_FEED1;
	LPC_SC->PLL0FEED = PLLFEED_FEED2;
	
	/* Set clock divider. */
	LPC_SC->CCLKCFG = 0x03;
	
	/* Configure flash accelerator. */
	LPC_SC->FLASHCFG = 0x403A;
	
	/* Check lock bit status. */
	while( ( ( LPC_SC->PLL0STAT & ( 1 << 26 ) ) == 0 ) );
	    
	/* Enable and connect. */
	LPC_SC->PLL0CON = 3;
	LPC_SC->PLL0FEED = PLLFEED_FEED1;
	LPC_SC->PLL0FEED = PLLFEED_FEED2;
	while( ( ( LPC_SC->PLL0STAT & ( 1 << 25 ) ) == 0 ) );
	
	/* Enable Clock Output to 50MHz */
	LPC_SC->CLKOUTCFG = 0x110;

//	/* Configure the clock for the USB. */
//
//	if( LPC_SC->PLL1STAT & ( 1 << 9 ) )
//	{
//		/* Enable PLL, disconnected. */
//		LPC_SC->PLL1CON = 1;
//		LPC_SC->PLL1FEED = PLLFEED_FEED1;
//		LPC_SC->PLL1FEED = PLLFEED_FEED2;
//	}
//
//	/* Disable PLL, disconnected. */
//	LPC_SC->PLL1CON = 0;
//	LPC_SC->PLL1FEED = PLLFEED_FEED1;
//	LPC_SC->PLL1FEED = PLLFEED_FEED2;
//
//	LPC_SC->PLL1CFG = 0x23;
//	LPC_SC->PLL1FEED = PLLFEED_FEED1;
//	LPC_SC->PLL1FEED = PLLFEED_FEED2;
//
//	/* Enable PLL, disconnected. */
//	LPC_SC->PLL1CON = 1;
//	LPC_SC->PLL1FEED = PLLFEED_FEED1;
//	LPC_SC->PLL1FEED = PLLFEED_FEED2;
//	while( ( ( LPC_SC->PLL1STAT & ( 1 << 10 ) ) == 0 ) );
//
//	/* Enable and connect. */
//	LPC_SC->PLL1CON = 3;
//	LPC_SC->PLL1FEED = PLLFEED_FEED1;
//	LPC_SC->PLL1FEED = PLLFEED_FEED2;
//	while( ( ( LPC_SC->PLL1STAT & ( 1 << 9 ) ) == 0 ) );

}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
	/* This function will get called if a task overflows its stack. */

	( void ) pxTask;
	( void ) pcTaskName;

	for( ;; );
}
/*-----------------------------------------------------------*/

void vConfigureTimerForRunTimeStats( void )
{
const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;

	/* This function configures a timer that is used as the time base when
	collecting run time statistical information - basically the percentage
	of CPU time that each task is utilising.  It is called automatically when
	the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set
	to 1). */

	/* Power up and feed the timer. */
	LPC_SC->PCONP |= 0x02UL;
	LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);

	/* Reset Timer 0 */
	LPC_TIM0->TCR = TCR_COUNT_RESET;

	/* Just count up. */
	LPC_TIM0->CTCR = CTCR_CTM_TIMER;

	/* Prescale to a frequency that is good enough to get a decent resolution,
	but not too fast so as to overflow all the time. */
	LPC_TIM0->PR =  ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;

	/* Start the counter. */
	LPC_TIM0->TCR = TCR_COUNT_ENABLE;
}
/*-----------------------------------------------------------*/

